package com.open.ssm.jasypt;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jasypt.encryption.StringEncryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.open.ssm.common.utils.StringUtils;

/**
 * Jasypt加解密Controller
 * 
 * @version: v1.0.0
 * @author: wangpf
 * @date: 2020年2月1日 下午1:55:43
 */
@RestController
@RequestMapping(value = "/jasypt/jencryptor")
public class JasyptEncryptorController {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    private StringEncryptor codeSheepEncryptorBean;
	
	// 加密
	@RequestMapping(value = "encrypt/{data}")
	public String encrypt(@PathVariable String data, HttpServletRequest request, HttpServletResponse response) {
		if(StringUtils.isBlank(data)) {
			return "originStr is null";
		}
		
		String encryptStr = codeSheepEncryptorBean.encrypt(data);
        return encryptStr;
	}
	
	// 解密
	@RequestMapping(value = "decrypt/{data}")
	public String decrypt(@PathVariable String data, HttpServletRequest request, HttpServletResponse response) {
		if(StringUtils.isBlank(data)) {
			return "originStr is null";
		}
		
		try {
			String decryptStr = codeSheepEncryptorBean.decrypt(data);
			return decryptStr;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return e.getMessage();
		}
	}
}
